package ps.client.plugin.eq2.log;

import java.util.Properties;
import java.util.Vector;
import java.util.regex.Pattern;


public class Trigger {
	
	public static final int PARSE_ALL = 0;
	public static final int IGNORE_CHAT = 1;
	public static final int CHAT_ONLY = 2;
	public static final int CHAT_MSG_ONLY = 4;
	
	static Properties regexProperties = new Properties();
	static Vector<Trigger> globalTriggerList = new Vector<Trigger>();
	
	public static final Trigger TIME_STAMP = new Trigger("TimeStamp", "\\(\\d{10}\\)\\[.{24}\\].*");
//	public static final Trigger CHAT = new Trigger("Chat", "(\\\\a.*/a|You)\\s(tell|say|shout).*,\\s\".*\"");
	public static final Trigger CHAT = new Trigger("Chat", "(\\\\a.*/a|Ihr)\\s(sagt|teilt|ruft).*,\\s?\".*\"");
	
	public static final Trigger HTTP_LINK = new Trigger("HttpLink", ".*(http://|www\\.).*", CHAT_MSG_ONLY);
	public static final Trigger ITEM_LINK = new Trigger("ItemLink", "\\aITEM .*\\/a", CHAT_MSG_ONLY);
	
	public static final TraumaticSwipeTrigger TRAUMATIC_SWIPE = new TraumaticSwipeTrigger();
	public static final DeathTrigger DEATH = new DeathTrigger();
	
	public static final Trigger RAT_TIMER_START = new Trigger("RatTimerStart", "Unknown command: 'rat timer start'", IGNORE_CHAT);
	public static final Trigger RAT_TIMER_RESET = new Trigger("RatTimerReset", "Unknown command: 'rat timer reset'", IGNORE_CHAT);

	public static final Trigger GYNOKS_TOUCH_TRIGGER = new Trigger("GynoksTouch", "(Gynok Moltor's Touch of Death.*|Unknown command: 'rat gynok touch')", IGNORE_CHAT, 20);
	public static final Trigger YKESHAS_TOUCH = new Trigger("YkeshasTouch", "(Ykesha's Ykesha's Touch.*|Unknown command: 'rat ykesha touch')", IGNORE_CHAT, 20);
	public static final Trigger YKESHAS_WHIRLING_BLADES = new Trigger("YkeshasWhirlingBlades", "(Ykesha's Whirling Blades.*|Unknown command: 'rat ykesha wb')", IGNORE_CHAT, 20);
	
	String name;
	String regex;
	Pattern pattern;
	
	int flags = PARSE_ALL;
	
	boolean enabled = false;
	boolean hitBefore = false;
	long lastHit = 0;
	int minSecondsBetweenHits = 0;
	
	Vector<TriggerListener> listenerList = new Vector<TriggerListener>();
	
	public Trigger(String name, String regex) {
		this(name, regex, PARSE_ALL, 0);
	}
	
	public Trigger(String name, String regex, int flags) {
		this(name, regex, flags, 0);
	}
	
	public Trigger(String name, String regex, int flags, int minSecondsBetweenHits) {
//		if (regexProperties == null) {
//			regexProperties = new Properties();
//		}
		
		this.name = name;
		this.regex = regexProperties.getProperty(name, regex);
		this.pattern = Pattern.compile(regex);
		this.flags = flags;
		this.minSecondsBetweenHits = minSecondsBetweenHits;
	}
	
	public void addTriggerListener(TriggerListener listener) {
		if (!listenerList.contains(listener)) {
			listenerList.add(listener);
		}
		if (!globalTriggerList.contains(this)) {
			globalTriggerList.add(this);
		}
		if (listenerList.size() > 0) {
			enabled = true;
		}
	}
	
	public void removeTriggerListener(TriggerListener listener) {
		listenerList.remove(listener);
		if (listenerList.size() == 0) {
			enabled = false;
		}
	}
	
	public TriggerListener[] getTriggerListener() {
		TriggerListener[] ret = new TriggerListener[listenerList.size()];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = listenerList.get(i);
		}
		return ret;
	}
	
	public void fireTriggerEvent(TriggerEvent e) {
		for (int i = 0; i < listenerList.size(); i++) {
			listenerList.get(i).triggerEvent(e);
		}
		hitBefore = true;
		lastHit = System.currentTimeMillis();
	}
	
	public int getSecondsToLastHit() {
		return (int)((System.currentTimeMillis() - lastHit) / 1000);
	}
	
	public void resetTimer() {
		hitBefore = false;
		lastHit = 0;
	}
	
	public String getRegex() {
		return regex;
	}
	
	public Pattern getPattern() {
		return pattern;
	}
	
	public int getFlags() {
		return flags;
	}
	
	public boolean isHitBefore() {
		return hitBefore;
	}
	
	public long getLastHit() {
		return lastHit;
	}
	
	public int getMinSecondsBetweenHits() {
		return minSecondsBetweenHits;
	}
	
	public boolean isEnabled() {
		return enabled;
	}
	
	public static void removeListenerFromAllTrigger(TriggerListener listener) {
		for (int i = 0; i < globalTriggerList.size(); i++) {
			globalTriggerList.get(i).removeTriggerListener(listener);
		}
	}
	
}
